export default {
  computed: {},
  data() {
    return {
      sortColumnsList: [],
      fieldsOptionMenus: [],
      titleRightDefaultMenus: [
        // 默认预制按钮
        {
          key: 'asc',
          name: this.$i18n('cap.unflow.kanban.asc'),
          valueHandel: (column) => column.ascValue,
          click: this.ascHandel
        },
        {
          key: 'desc',
          name: this.$i18n('cap.unflow.kanban.desc'),
          valueHandel: (column) => column.descValue,
          click: this.descHandel
        },
        {
          key: 'freeze',
          name: this.$i18n('cap.template.component.table.frozenCol'),
          filter: (menu) => {
            // 最后一个列，不能冻结
            if (this.calcColumns && this.calcColumns.filter((col) => !col.fixed).length == 1) {
              return false;
            }
            return !menu.fixed;
          },
          click: this.freezeColumn
        },
        {
          key: 'freeze',
          name: this.$i18n('cap.template.component.table.unFrozen'),
          filter: (menu) => menu.fixed,
          click: this.freezeColumn
        },
        {
          key: 'fieldset',
          name: this.$i18n('cap.deduct.field.settings'),
          childrenMenuStyle: {
            width: '184px'
          }
        }
      ]
    };
  },
  created() {
    this.sortColumnsObj = {};
  },
  methods: {
    getSliceTitleRightMenus(column) {
      let menus = [];
      if (column.dropdownMenus) {
        menus = this.titleRightDefaultMenus.filter((menu) => {
          let menuOption = column.dropdownMenus[menu.key];
          if (typeof menuOption === 'object') {
            return true;
          } else {
            return menuOption;
          }
        });
      }
      return menus;
    },
    updataColumns(fieldsOptionMenus) {
      let catchRightDefaultMenusObj = fieldsOptionMenus
        ? fieldsOptionMenus
            .filter((el) => el.value)
            .map((el) => el.colKey)
            .join(',')
        : this.catchRightDefaultMenusObj;
      if (catchRightDefaultMenusObj != this.catchRightDefaultMenusObj) {
        this.catchRightDefaultMenusObj = catchRightDefaultMenusObj;
        this.fieldsOptionMenus = fieldsOptionMenus;
        this.initColumnsSortMap = {};
        fieldsOptionMenus.forEach((element, index) => {
          this.initColumnsSortMap[element.colKey] = index;
        });
        // 更新初始化顺序
        this.initColumnsArray = this.initColumnsArray.sort(
          (a, b) => this.initColumnsSortMap[a.key] - this.initColumnsSortMap[b.key]
        );
        // 存储到本地
        this.saveFieldsOptionMenus();
        // 调整显示
        this.initColumnsDisplay();
        // 重新调整顺序
        this.initColumnsSort(true);
        // 修正宽度
        this.initAfterAutoColumnsWidth();
        // 更新滚动条
        this.$refs.tableScroll && this.$refs.tableScroll.reset();
        this.$refs.tableScrollX && this.$refs.tableScrollX.reset();
      }
    },
    freezeColumn(column) {
      let { calcColumns } = this;
      let key = column.key;
      const col = calcColumns.find((col) => {
        const k = col.key;
        if (k === key) {
          return true;
        }
      });
      if (!column.fixed) {
        col.fixed = 'left';
        // 提前
        this.initFieldsOptionMenus(col, true);
      } else {
        col.fixed = false;
        // 回归
        this.initFieldsOptionMenus(col, false);
      }
      this.initColumnsDisplay();
      // this.initColumnsSort();
      this.$nextTick(() => {
        this.initAfterEmptyHeight();
        this.initAfterScrollTableBody();
        this.scrollYHandel(this.scrollBarTop);
      });
    },
    sortHandel(column) {
      if (column.dropdownMenus && (column.dropdownMenus.asc || column.dropdownMenus.desc)) {
        if (!column.ascValue && !column.descValue) {
          this.ascHandel(column);
        } else {
          if (column.ascValue) {
            this.descHandel(column);
          } else if (!column.ascValue && column.descValue) {
            // 取消降序
            this.descHandel(column);
          }
        }
      }
    },
    ascHandel(column) {
      column = this.initColumnsArray.find((col) => col.key === column.key);
      let sortColumnsObj = this.sortColumnsObj;
      if (!sortColumnsObj[column.key]) {
        sortColumnsObj[column.key] = 'asc';
        column.ascValue = true;
        // 把自己的规则放在后面
        this.sortColumnsList = [
          ...this.sortColumnsList,
          { key: column.key, dataIndex: column.dataIndex }
        ];
      } else {
        if (sortColumnsObj[column.key] === 'desc') {
          // 移除自己排序规则
          this.sortColumnsList = this.sortColumnsList.filter((col) => col.key !== column.key);
          this.sortColumnsList = [
            ...this.sortColumnsList,
            { key: column.key, dataIndex: column.dataIndex }
          ];
          column.ascValue = true;
          column.descValue = false;
          sortColumnsObj[column.key] = 'asc';
        } else {
          delete sortColumnsObj[column.key];
          column.ascValue = false;
          // 移除自己排序规则
          this.sortColumnsList = this.sortColumnsList.filter((col) => col.key !== column.key);
        }
      }
      this.sortColumnsObj = sortColumnsObj;
    },
    descHandel(column) {
      column = this.initColumnsArray.find((col) => col.key === column.key);
      let sortColumnsObj = this.sortColumnsObj;
      if (!sortColumnsObj[column.key]) {
        sortColumnsObj[column.key] = 'desc';
        column.descValue = true;
        // 把自己的规则放在后面
        this.sortColumnsList = [
          ...this.sortColumnsList,
          { key: column.key, dataIndex: column.dataIndex }
        ];
      } else {
        if (sortColumnsObj[column.key] === 'asc') {
          // 移除自己排序规则
          this.sortColumnsList = this.sortColumnsList.filter((col) => col.key !== column.key);
          this.sortColumnsList = [
            ...this.sortColumnsList,
            { key: column.key, dataIndex: column.dataIndex }
          ];
          column.descValue = true;
          column.ascValue = false;
          sortColumnsObj[column.key] = 'desc';
        } else {
          delete sortColumnsObj[column.key];
          column.descValue = false;
          // 移除自己排序规则
          this.sortColumnsList = this.sortColumnsList.filter((col) => col.key !== column.key);
        }
      }
      this.sortColumnsObj = sortColumnsObj;
    }
  },

  mounted() {}
};
